(function (vc, vm) {
    vc.extends({
        data: {
            editHousekeepingResourceInfo: {
                resId: '',
                resTypeId: '',
                resTypeName: '',
                resourceStoreTypes: [],
                resName: '',
                resCode: '',
                costPrice: '',
                price: '',
                stock: '',
                supplierId: '',
                supplierName: '',
                feeTypeCd: '',
                resourceSuppliers: [],
                remark: '',
                configId: '',
                housekeepingFeeTypes: []
            }
        },
        _initMethod: function () {
            //$that._listEditResourceStoreTypes();
            $that._listEditResourceSuppliers();
            $that._listEditResourceStoreTypes();
        },
        _initEvent: function () {
            vc.on('editHousekeepingResource', 'openEditHousekeepingResourceModal', function (_params) {
                $that.refreshEditHousekeepingResourceInfo();
                $('#editHousekeepingResourceModel').modal('show');
                vc.copyObject(_params, $that.editHousekeepingResourceInfo);
            });
        },
        methods: {
            editHousekeepingResourceValidate: function () {
                return vc.validate.validate({
                    editHousekeepingResourceInfo: $that.editHousekeepingResourceInfo
                }, {
                    'editHousekeepingResourceInfo.resTypeId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "物品类型ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "物品类型ID太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.resTypeName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "物品类型名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "物品类型名称太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.resName': [
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "物品名称太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.resCode': [
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "物品编码太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.costPrice': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "成本价不能为空"
                        }
                    ],
                    'editHousekeepingResourceInfo.price': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "出售价不能为空"
                        }
                    ],
                    'editHousekeepingResourceInfo.stock': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "库存不能为空"
                        }
                    ],
                    'editHousekeepingResourceInfo.supplierId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "供应商ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "供应商ID太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.supplierName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "供应商名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "供应商名称太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "描述太长"
                        },
                    ],
                    'editHousekeepingResourceInfo.resId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "服务物品ID不能为空"
                        }
                    ]
                });
            },
            editHousekeepingResource: function () {
                if (!$that.editHousekeepingResourceValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.editHousekeepingResourceInfo.shopId = vc.getCurrentCommunity().shopId;
                vc.http.apiPost(
                    '/housekeepingResource/updateHousekeepingResource',
                    JSON.stringify($that.editHousekeepingResourceInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editHousekeepingResourceModel').modal('hide');
                            vc.emit('housekeepingResourceManage', 'listHousekeepingResource', {});
                            vc.toast("修改成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            //查询方法
            _listEditResourceStoreTypes: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50
                    }
                };
                //发送post请求
                vc.http.apiGet(
                    'resourceStoreType.listResourceStoreTypes',
                    param,
                    function (json, res) {
                        var _resourceStoreTypeManageInfo = JSON.parse(json);
                        $that.editHousekeepingResourceInfo.resourceStoreTypes = _resourceStoreTypeManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listEditResourceSuppliers: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50
                    }
                };
                //发送get请求
                vc.http.apiGet(
                    'resourceSupplier.listResourceSuppliers',
                    param,
                    function (json, res) {
                        var _resourceSupplierManageInfo = JSON.parse(json);
                        $that.editHousekeepingResourceInfo.resourceSuppliers = _resourceSupplierManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            setEditResTypeName: function (_resTypeId) {
                $that.editHousekeepingResourceInfo.resourceStoreTypes.forEach(item => {
                    if (item.rstId == _resTypeId) {
                        $that.editHousekeepingResourceInfo.resTypeName = item.name;
                    }
                });
            },
            setEditSupplierName: function (_supplierId) {
                $that.editHousekeepingResourceInfo.resourceSuppliers.forEach(item => {
                    if (item.rsId == _supplierId) {
                        $that.editHousekeepingResourceInfo.supplierName = item.supplierName;
                    }
                });
            },
            refreshEditHousekeepingResourceInfo: function () {
                let _resourceStoreTypes = $that.editHousekeepingResourceInfo.resourceStoreTypes;
                let _resourceSuppliers = $that.editHousekeepingResourceInfo.resourceSuppliers;
                let _housekeepingFeeTypes = $that.editHousekeepingResourceInfo.housekeepingFeeTypes;
                $that.editHousekeepingResourceInfo = {
                    resId: '',
                    resTypeId: '',
                    resTypeName: '',
                    resourceStoreTypes: _resourceStoreTypes,
                    resName: '',
                    resCode: '',
                    costPrice: '',
                    price: '',
                    stock: '',
                    supplierId: '',
                    supplierName: '',
                    feeTypeCd: '',
                    configId: '',
                    resourceSuppliers: _resourceSuppliers,
                    remark: '',
                    housekeepingFeeTypes: _housekeepingFeeTypes
                }
            }
        }
    });
})(window.vc, window.vc.component);
